package com.meilai.project.service.system.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.system.MediaTypeClassificationDTO;
import com.meilai.project.entity.media.MediaType;
import com.meilai.project.entity.system.MediaTypeClassification;
import com.meilai.project.mapper.system.MediaTypeClassificationMapper;
import com.meilai.project.service.business.media.MediaTypeService;
import com.meilai.project.service.system.MediaTypeClassificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.vo.system.MediaTypeClassificationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 媒体类型分类表 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2022-05-20
 */
@Service
public class MediaTypeClassificationImpl extends ServiceImpl<MediaTypeClassificationMapper, MediaTypeClassification> implements MediaTypeClassificationService {

    @Autowired
    private MediaTypeService mediaTypeService;

    @Override
    public Page<MediaTypeClassificationVO> page(Integer current_page, Integer page_size, String name) {
        Page<MediaTypeClassificationVO> page = new Page<>(current_page, page_size);
        page.setRecords(baseMapper.page(page, name));
        return page;
    }

    @Override
    public Boolean save(MediaTypeClassificationDTO dto) {
        MediaTypeClassification entity = new MediaTypeClassification();
        BeanUtils.copyProperties(dto, entity);
        return save(entity);
    }

    @Override
    public Boolean edit(MediaTypeClassificationDTO dto) {
        MediaTypeClassification entity = new MediaTypeClassification();
        BeanUtils.copyProperties(dto, entity);
        return updateById(entity);
    }

    @Override
    public List<MediaTypeClassification> list(Long media_type_id) {
        MediaType mediaType = mediaTypeService.getById(media_type_id);
        return list(Wrappers.<MediaTypeClassification>lambdaQuery()
                .isNull(MediaTypeClassification::getDeleted_at).and(i -> i.eq(MediaTypeClassification::getMedia_type_pid, media_type_id).or().eq(MediaTypeClassification::getMedia_type_pid, mediaType.getPid())));
    }
}
